#http://122.235.240.91/pzgl/sys/peizhigl/export?bbxh=123&SQLTYPE=&type=oracle&t=1464165526899

from urllib.error import URLError,HTTPError  
import urllib.parse  
import urllib.request
import re
import json
import os

import login
import dlsver

directory = "D:\dev\Pzgl"
dbtype = ["oracle","mssql"]

def mkdir():
   
   if os.path.exists(directory) == False:
      os.makedirs(directory)
   
   for x in dbtype:
      if os.path.exists(directory+"\\%s" % x) == False:
          os.makedirs(directory+"\\%s" % x)


def doDownLoad():

    mkdir()
    

    rows = dlsver.getVersion()
    try:  
       
       for one in rows:
            for db in dbtype:

                bbxh = one["BBXH"]
                bbmc = one["BBMC"]
                print("正在下载：%s (%s)" % (bbmc , db))
                url = "http://%s/sys/peizhigl/export?bbxh=%s&p=1&xtxx=01&SQLTYPE=&type=%s&t=1234646464" % (login.getIpAddress(),bbxh,db)
                data = urllib.request.urlopen(url).read()
                strdata = str(data, encoding = "utf-8")
                fo = open("%s\\%s\\%s.txt" % (directory,db,bbmc) , "w")
                fo.write(strdata)

                print("下载完成：%s (%s)" % (bbmc , db))

    except HTTPError as e:  
        print('Error code:',e.code)   
    except URLError as e:  
        print('Reason',e.reason)